<template>
  <div class="my1bg">
    <div style="height:20px;"></div>
    <PathThinkItem
      :processId="22"
      :sideName="'工序管理'"
      :ifAdd="false"
      :rightAddFormModel="addFormModel"
      :rightAddConfig="addRightConfig"
      :leftAddFormModel="addFormModel"
      :leftAddConfig="addLeftConfig"
      :leftNodeList="leftNodeList"
      :centerNodeList="centerNodeList"
      :rightNodeList="rightNodeList"
      @add="addItem"
      @turn="turn"
    />
  </div>
</template>

<script>
import PathThinkItem from "@/components/PathThink/PathThinkItem"

export default {
  components: {
    PathThinkItem
  },
  mounted() {
    // 页面初始化，获取processId
    this.processId = this.$route.query.processId
    this.centerNodeList[0].name = this.$route.query.name

    if (this.$route.query.emissionId) {
      this.linkEmissionAndProcess(this.$route.query.emissionId)
    } else {
      this.getEmissionAndProcess()
    }
    this.getEmissionRecommend();
    this.test()
  },
  data() {
    return {
      testFlag: true,
      LeftNameList: ['原材料/物料', "耗材", "能源", "资源", "运输/通勤", "固碳", "包装材料"],
      processId: "",
      centerName: "",
      addLeftConfig: [
        {
          id: '1',
          type: 'input',
          model: 'name',
          name: '排放源名称',
          edit: true,
          props: {
            placeholder: '排放源名称',
          }
        },
        {
          id: '2',
          type: 'select',
          model: 'type',
          name: '排放类型',
          edit: true,
          props: {
            placeholder: '排放类型',
            options: [
              {value: '原材料/物料', label: '原材料/物料'},
              {value: '耗材', label: '耗材'},
              {value: '能源', label: '能源'},
              {value: '资源', label: '资源'},
              {value: '运输/通勤', label: '运输/通勤'},
              {value: '固碳', label: '固碳'},
              {value: '再生原料', label: '再生原料'},
              {value: '包装材料', label: '包装材料'},
            ]
          }
        },
        {
          id: '3',
          type: 'input',
          model: 'note',
          name: '排放源说明',
          edit: true,
          props: {
            placeholder: '排放源说明',
          }
        },
        {
          id: '4',
          type: 'select',
          model: 'emissionsUnitN',
          name: '排放单位分子',
          edit: true,
          props: {
            placeholder: '排放单位分子',
            options: [
              {value: "tCO2-eq", label: "tCO2-eq"},
              {value: "kgCO2-eq", label: "kgCO2-eq"},
            ]
          }
        },
        {
          id: '5',
          type: 'select',
          model: 'emissionsUnitD',
          name: '排放单位分母',
          edit: true,
          props: {
            placeholder: '排放单位分母',
            options: [
              {value: "t", label: "t"},
              {value: "kg", label: "kg"},
              {value: "t·km", label: "t·km"},
              {value: "kwh", label: "kwh"}
            ]
          }
        },
        {
          id: '6',
          type: 'select',
          model: 'matchType',
          name: '匹配类型',
          edit: true,
          props: {
            placeholder: '匹配类型',
            options: [
              {value: '排放因子', label: '排放因子'},
              {value: '检测值', label: '检测值'}
            ]
          }
        },
        {
          id: '7',
          type: 'input',
          model: 'factor',
          name: '因子名',
          edit: true,
          props: {
            placeholder: '因子名',
          }
        },
        {
          id: '8',
          type: 'input',
          model: 'factorEmissions',
          name: '排放因子值',
          edit: true,
          props: {
            placeholder: '排放因子值',
          }
        },
        {
          id: '9',
          type: 'input',
          model: 'dataSource',
          name: '数据来源',
          edit: true,
          props: {
            placeholder: '数据来源',
          }
        },
      ],
      addRightConfig: [
        {
          id: '1',
          type: 'input',
          model: 'dataSource',
          name: '数据来源',
          edit: true,
          props: {
            placeholder: '数据来源',
          }
        },
        {
          id: '2',
          type: 'select',
          model: 'emissionsUnitD',
          name: '排放单位分母',
          edit: true,
          props: {
            placeholder: '排放单位分母',
            options: [
              {value: "t", label: "t"},
              {value: "kg", label: "kg"},
            ]
          }
        },
        {
          id: '3',
          type: 'select',
          model: 'emissionsUnitN',
          name: '排放单位分子',
          edit: true,
          props: {
            placeholder: '排放单位分子',
            options: [
              {value: "tCO2-eq", label: "tCO2-eq"},
              {value: "kgCO2-eq", label: "kgCO2-eq"},
            ]
          }
        },
        {
          id: '4',
          type: 'input',
          model: 'factor',
          name: '因子名',
          edit: true,
          props: {
            placeholder: '因子名',
          }
        },
        {
          id: '5',
          type: 'input',
          model: 'factorEmissions',
          name: '排放因子值',
          edit: true,
          props: {
            placeholder: '排放因子值',
          }
        },
        {
          id: '7',
          type: 'select',
          model: 'matchType',
          name: '匹配类型',
          edit: true,
          props: {
            placeholder: '匹配类型',
            options: [
              {value: '排放因子', label: '排放因子'},
              {value: '检测值', label: '检测值'}
            ]
          }
        },
        {
          id: '8',
          type: 'input',
          model: 'name',
          name: '排放源名称',
          edit: true,
          props: {
            placeholder: '排放源名称',
          }
        },
        {
          id: '9',
          type: 'input',
          model: 'note',
          name: '排放源说明',
          edit: true,
          props: {
            placeholder: '排放源说明',
          }
        },
        {
          id: '10',
          type: 'select',
          model: 'type',
          name: '类型',
          edit: true,
          props: {
            placeholder: '类型',
            options: [
              {value: '废气', label: '废气'},
              {value: '废水', label: '废水'},
              {value: '固废', label: '固废'},
              {value: '可再生废料', label: '可再生废料'},
            ]
          }
        },
      ],
      addFormModel: {
        name: "", //	排放源名称
        type: "", //		类型
        matchType: "", //		匹配类型
        note: "", //		排放源说明
        factor: "", //		因子名
        factorEmissions: "", //		排放因子值
        emissionsUnitN: "", //		排放单位分子
        emissionsUnitD: "", //		排放单位分母
        dataSource: "", //		数据来源
      },
      plus: {},
      leftNodeList: [
        []
        // [{name:"铁矿石", img:'s8.png'},{name:"焦炭"},{name:"燃料"},{name:"萤石"},{name:"方解石"},]
      ],
      centerNodeList: [
        {name: "焙烧"}
      ],
      rightNodeList: [
        []
        // [{name:"烧结矿"},{name:"烟气"},{name:"热能"},{name:"废渣"},]
      ]

    }
  },
  methods: {
    test() {
      setInterval(() => {
        console.log(this.testFlag)
        if (this.testFlag) {
          this.leftNodeList[0].push({name: '石灰'})
          this.testFlag = false
        }
      }, 8000);
    },
    // 推荐排放源
    getEmissionRecommend() {
      this.$API.product.getEmissionRecommend({
        id: this.processId,
      }).then(response => {
      })
    },
    // 添加已经添加的排放源
    turn() {
      this.$router.push({
        path: "/carbonFootprintVerification/carbonFootprintVerification/emissionSource",
        query: {
          select: true,
          processId: this.processId,
          name: this.centerNodeList[0].name,
          type: 1
        }
      })
    },
    // 处理添加事件
    addItem(type, model) {
      if (type == "left") { // 添加左节点
        this.createProcessAndEmission(model)
      } else if (type == "right") { // 添加右节点
        this.createProcessAndEmission(model)
      }
    },
    // 创建排放源 绑定排放源和工序
    createProcessAndEmission(model) {
      this.$API.product.createEmission({
        ...model,
      }).then(response => {
        // 创建排放源和工序的连接
        this.linkEmissionAndProcess(response.data)
      })
    },
    // 绑定排放源和工序
    linkEmissionAndProcess(emissionId) {
      this.$API.product.linkEmissionAndProcess({
        emissionId: emissionId,
        processId: this.processId
      }).then(response => {
        this.getEmissionAndProcess();
      })
    },
    // 获得排放源-工序关联
    getEmissionAndProcess() {
      this.$API.product.getEmissionAndProcess({
        id: this.processId
      }).then(response => {
        var data = response.data.emissions
        this.leftNodeList = [[]]
        this.rightNodeList = [[]]
        for (var i = 0; i < data.length; i++) {
          if (this.LeftNameList.indexOf(data[i].type) != -1) {
            this.leftNodeList[0].push({
              ...data[i],
              dan: data[i].emissionsUnitN + "/" + data[i].emissionsUnitD
            })
          } else {
            this.rightNodeList[0].push({
              ...data[i],
              dan: data[i].emissionsUnitN + "/" + data[i].emissionsUnitD
            })
          }
        }
      })
    }

  }
}
</script>

<style lang="less" scoped>
.my1bg {
  background-color: #fff;
}
</style>
